<html lang="zh-CN"><head><meta charset="UTF-8"><style>.nodata  main {width:1000px;margin: auto;}</style></head><body class="nodata " style=""><div class="main_father clearfix d-flex justify-content-center " style="height:100%;"> <div class="container clearfix " id="mainBox"><main><div class="blog-content-box">
<div class="article-header-box">
<div class="article-header">
<div class="article-title-box">
<h1 class="title-article" id="articleContentId">(A卷,100分)- 新员工座位（Java & JS & Python）</h1>
</div>
</div>
</div>
<div id="blogHuaweiyunAdvert"></div>

        <div id="article_content" class="article_content clearfix">
        <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/kdoc_html_views-1a98987dfd.css">
        <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/ck_htmledit_views-044f2cf1dc.css">
                <div id="content_views" class="htmledit_views">
                    <h4 id="main-toc">题目描述</h4> 
<p>工位由序列F1,F2…Fn组成&#xff0c;Fi值为0、1或2。其中0代表空置&#xff0c;1代表有人&#xff0c;2代表障碍物。</p> 
<p>1、某一空位的友好度为左右连续老员工数之和&#xff0c;<br /> 2、为方便新员工学习求助&#xff0c;优先安排友好度高的空位&#xff0c;</p> 
<p>给出工位序列&#xff0c;求所有空位中<strong>友好度的最大值</strong>。</p> 
<p></p> 
<h4 id="%E8%BE%93%E5%85%A5%E6%8F%8F%E8%BF%B0">输入描述</h4> 
<p>第一行为工位序列&#xff1a;F1&#xff0c;F2…Fn组成&#xff0c;<br /> 1&lt;&#61;n&lt;&#61;10000&#xff0c;Fi值为0、1或2。其中0代表空置&#xff0c;1代表有人&#xff0c;2代表障碍物。</p> 
<p></p> 
<h4 id="%E8%BE%93%E5%87%BA%E6%8F%8F%E8%BF%B0">输出描述</h4> 
<p>所有空位中友好度的最大值。如果没有空位&#xff0c;返回0。</p> 
<p></p> 
<h4 id="%E7%94%A8%E4%BE%8B">用例</h4> 
<table border="1" cellpadding="1" cellspacing="1" style="width:500px;"><tbody><tr><td style="width:86px;">输入</td><td style="width:412px;">0 1 0</td></tr><tr><td style="width:86px;">输出</td><td style="width:412px;">1</td></tr><tr><td style="width:86px;">说明</td><td style="width:412px;">第1个位置和第3个位置&#xff0c;友好度均为1。</td></tr></tbody></table> 
<table border="1" cellpadding="1" cellspacing="1" style="width:500px;"><tbody><tr><td style="width:86px;">输入</td><td style="width:412px;">1 1 0 1 2 1 0</td></tr><tr><td style="width:86px;">输出</td><td style="width:412px;">3</td></tr><tr><td style="width:86px;">说明</td><td style="width:412px;">第3个位置友好度为3。因障碍物隔断&#xff0c;左边得2分&#xff0c;右边只能得1分。</td></tr></tbody></table> 
<h4 id="%E9%A2%98%E7%9B%AE%E8%A7%A3%E6%9E%90">题目解析</h4> 
<p>本题最优解题思路如下&#xff1a;</p> 
<p>定义一个变量friendShip &#61; 0&#xff0c;</p> 
<p>首先对输入数组进行正向遍历&#xff08;从左到右&#xff09;&#xff1a;</p> 
<ul><li>遇到“1”&#xff0c;则friendShip&#43;&#43;</li><li>遇到“0”&#xff0c;则说明该空位左边的友好度为friendShip&#xff0c;记录下来&#xff0c;然后将friendShip重置为0</li><li>遇到“2”&#xff0c;则直接将friendShip重置为0</li></ul> 
<p>这样每个空位的左边的友好度就统计出来了。</p> 
<p></p> 
<p>接着&#xff0c;将friendShip重置为0&#xff0c;</p> 
<p>然后对输入数组进行反向遍历&#xff08;从右到左&#xff09;&#xff1a;</p> 
<ul><li>遇到“1”&#xff0c;则friendShip&#43;&#43;</li><li>遇到“0”&#xff0c;则说明该空位右边的友好度为friendShip&#xff0c;累加下来&#xff0c;然后将friendShip重置为0</li><li>遇到“2”&#xff0c;则直接将friendShip重置为0</li></ul> 
<p>这样每个空位的整体友好度就统计出来了&#xff0c;取最大值返回。</p> 
<p></p> 
<h4 id="%E7%AE%97%E6%B3%95%E6%BA%90%E7%A0%81">JavaScript算法源码</h4> 
<pre><code class="language-javascript">/* JavaScript Node ACM模式 控制台输入获取 */
const readline &#61; require(&#34;readline&#34;);

const rl &#61; readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});

rl.on(&#34;line&#34;, (line) &#61;&gt; {
  const arr &#61; line.split(&#34; &#34;);
  console.log(getResult(arr));
});

function getResult(arr) {
  // 记录空位的友好度
  const ep &#61; {};

  let friendShip &#61; 0;
  // 从左向右遍历&#xff0c;记录每个空位左边的友好度
  for (let i &#61; 0; i &lt; arr.length; i&#43;&#43;) {
    switch (arr[i]) {
      case &#34;0&#34;:
        ep[i] &#61; friendShip;
        friendShip &#61; 0;
        break;
      case &#34;1&#34;:
        friendShip&#43;&#43;;
        break;
      case &#34;2&#34;:
        friendShip &#61; 0;
        break;
    }
  }

  friendShip &#61; 0;
  let ans &#61; 0;
  // 从右向左遍历&#xff0c;累加每个空位右边的友好度&#xff0c;这样就得到了每个空位的友好度&#xff0c;取最大值即可
  for (let i &#61; arr.length - 1; i &gt;&#61; 0; i--) {
    switch (arr[i]) {
      case &#34;0&#34;:
        ans &#61; Math.max(ans, ep[i] &#43; friendShip);
        friendShip &#61; 0;
        break;
      case &#34;1&#34;:
        friendShip&#43;&#43;;
        break;
      case &#34;2&#34;:
        friendShip &#61; 0;
        break;
    }
  }

  return ans;
}
</code></pre> 
<p></p> 
<h4>Java算法源码</h4> 
<pre><code class="language-java">import java.util.HashMap;
import java.util.Scanner;

public class Main {
  public static void main(String[] args) {
    Scanner sc &#61; new Scanner(System.in);

    String[] arr &#61; sc.nextLine().split(&#34; &#34;);

    System.out.println(getResult(arr));
  }

  public static int getResult(String[] arr) {
    // 记录空位的友好度
    HashMap&lt;Integer, Integer&gt; ep &#61; new HashMap&lt;&gt;();

    int friendShip &#61; 0;
    // 从左向右遍历&#xff0c;记录每个空位左边的友好度
    for (int i &#61; 0; i &lt; arr.length; i&#43;&#43;) {
      switch (arr[i]) {
        case &#34;0&#34;:
          ep.put(i, friendShip);
          friendShip &#61; 0;
          break;
        case &#34;1&#34;:
          friendShip&#43;&#43;;
          break;
        case &#34;2&#34;:
          friendShip &#61; 0;
          break;
      }
    }

    friendShip &#61; 0;
    int ans &#61; 0;
    // 从右向左遍历&#xff0c;累加每个空位右边的友好度&#xff0c;这样就得到了每个空位的友好度&#xff0c;取最大值即可
    for (int i &#61; arr.length - 1; i &gt;&#61; 0; i--) {
      switch (arr[i]) {
        case &#34;0&#34;:
          ans &#61; Math.max(ans, ep.get(i) &#43; friendShip);
          friendShip &#61; 0;
          break;
        case &#34;1&#34;:
          friendShip&#43;&#43;;
          break;
        case &#34;2&#34;:
          friendShip &#61; 0;
          break;
      }
    }

    return ans;
  }
}
</code></pre> 
<p></p> 
<h4>Python算法源码</h4> 
<pre><code class="language-python"># 输入获取
arr &#61; input().split()


# 算法入口
def getResult(arr):
    # 记录空位的友好度
    ep &#61; {}

    friendShip &#61; 0
    # 从左向右遍历&#xff0c;记录每个空位左边的友好度
    for i in range(len(arr)):
        if arr[i] &#61;&#61; &#34;0&#34;:
            ep[i] &#61; friendShip
            friendShip &#61; 0
        elif arr[i] &#61;&#61; &#34;1&#34;:
            friendShip &#43;&#61; 1
        else:
            friendShip &#61; 0

    friendShip &#61; 0
    ans &#61; 0
    # 从右向左遍历&#xff0c;累加每个空位右边的友好度&#xff0c;这样就得到了每个空位的友好度&#xff0c;取最大值即可
    for i in range(len(arr) - 1, -1, -1):
        if arr[i] &#61;&#61; &#34;0&#34;:
            ans &#61; max(ans, ep[i] &#43; friendShip)
            friendShip &#61; 0
        elif arr[i] &#61;&#61; &#34;1&#34;:
            friendShip &#43;&#61; 1
        else:
            friendShip &#61; 0

    return ans


# 算法调用
print(getResult(arr))</code></pre>
                </div>
        </div>
        <div id="treeSkill"></div>
        <div id="blogExtensionBox" style="width:400px;margin:auto;margin-top:12px" class="blog-extension-box"></div>
    <script>
  $(function() {
    setTimeout(function () {
      var mathcodeList = document.querySelectorAll('.htmledit_views img.mathcode');
      if (mathcodeList.length > 0) {
        for (let i = 0; i < mathcodeList.length; i++) {
          if (mathcodeList[i].naturalWidth === 0 || mathcodeList[i].naturalHeight === 0) {
            var alt = mathcodeList[i].alt;
            alt = '\\(' + alt + '\\)';
            var curSpan = $('<span class="img-codecogs"></span>');
            curSpan.text(alt);
            $(mathcodeList[i]).before(curSpan);
            $(mathcodeList[i]).remove();
          }
        }
        MathJax.Hub.Queue(["Typeset",MathJax.Hub]);
      }
    }, 1000)
  });
</script>
</div></html>